<template>
  <div>
    <!-- 面包屑-->
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-card>
      <!-- 搜索商品框 -->
      <el-row>
        <el-col class='col-search' :span='10'>
          <el-input placeholder='请输入商品名称' @clear='getGoodsList' clearable v-model='queryInfo.query'>
            <el-button slot='append' icon='el-icon-search' @click='getGoodsList'></el-button>
          </el-input>
        </el-col>
        <el-col :span='5'>
          <el-button type='primary' @click='goAddGoodsPage'>添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品数据表格 -->
      <el-row class='row-tab'>
        <!-- 商品数据表格 -->
        <el-table :data='goodsList' border stripe>
          <el-table-column type='index' align='center' label='#'></el-table-column>
          <el-table-column label='商品名称' align='center' prop='goods_name'></el-table-column>
          <el-table-column label='价格(元)' align='center' width='90px' prop='goods_price'></el-table-column>
          <el-table-column label='重量(kg)' align='center' width='80px' prop='goods_weight'></el-table-column>
          <el-table-column label='创建时间' align='center' width='150px'>
            <template v-slot='scope'>
              <span>{{ scope.row.add_time | formatTime }}</span>
            </template>
          </el-table-column>
          <el-table-column align='center' label='操作' width='180px'>
            <template v-slot='scope'>
              <el-button type='primary' size='mini' icon='el-icon-edit' @click='updateGoodsPull(scope.row)'>修改
              </el-button>
              <el-button type='danger' size='mini' icon='el-icon-delete' @click='delGoods(scope.row)'>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 修改商品数据 -->
        <el-dialog
          title='修改商品'
          :visible.sync='updateGoodsVisible'
          @close='closeUpdateForm'
          :close-on-click-modal='false'
          width='50%'>
          <el-form :model='updateGoodsForm'
                   :rules='updateGoodsRules'
                   :hide-required-asterisk='true'
                   label-width='100px'
                   ref='updateFormRef'>
            <el-form-item label='商品名称' prop='goods_name'>
              <el-input v-model='updateGoodsForm.goods_name'></el-input>
            </el-form-item>
            <el-form-item label='商品价格'>
              <el-input v-model='updateGoodsForm.goods_price' type='number' min='0'></el-input>
            </el-form-item>
            <el-form-item label='商品重量'>
              <el-input v-model='updateGoodsForm.goods_weight' type='number' min='0'></el-input>
            </el-form-item>
            <el-form-item label='商品数量'>
              <el-input v-model='updateGoodsForm.goods_number' type='number' min='0'></el-input>
            </el-form-item>
          </el-form>
          <span slot='footer' class='dialog-footer'>
    <el-button @click='updateGoodsVisible = false'>取 消</el-button>
    <el-button type='primary' @click='updateGoods'>确 定</el-button>
  </span>
        </el-dialog>
      </el-row>
      <!-- 分页 -->
      <el-row class='row-page'>
        <el-pagination
          @size-change='handleSizeChange'
          @current-change='handleCurrentChange'
          :current-page='queryInfo.pagenum'
          :page-sizes='[1, 3, 5, 10, 20]'
          :page-size='queryInfo.pagesize'
          background
          layout='total, sizes, prev, pager, next, jumper'
          :total='total'>
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsList',
  data() {
    return {
      // 获取商品数据的请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品数据
      goodsList: [],
      // 商品总数
      total: 0,
      // 弹出修改商品数据框
      updateGoodsVisible: false,
      // 修改商品数据的双向绑定
      updateGoodsForm: {
        // goods_name: '',
        // goods_price: 0,
        // goods_number: 0,
        // goods_weight: 0,
        // goods_introduce: '',
        // pics: [],
        // attrs: []
      },
      // 修改商品的验证规则
      updateGoodsRules: {
        goods_name: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品数据列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$msg.error('获取商品数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 跳转到添加商品页面
    goAddGoodsPage() {
      this.$router.push('/goods/add')
    },
    // 分页中改变每页条数
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 分页中当前页改变
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 拉起修改商品数据框
    async updateGoodsPull(row) {
      const { data: res } = await this.$http.get(`goods/${ row.goods_id }`)
      if (res.meta.status !== 200) {
        return this.$msg.error('获取商品数据失败')
      }
      this.updateGoodsForm = res.data
      this.updateGoodsVisible = true
    },
    // 修改商品数据
    updateGoods() {
      this.$refs.updateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`goods/${ this.updateGoodsForm.goods_id }`, this.updateGoodsForm)
        if (res.meta.status !== 200) {
          this.updateGoodsVisible = false
          return this.$msg.error('修改商品失败')
        }
        this.$msg.success('修改成功')
        this.updateGoodsVisible = false
        await this.getGoodsList()
      })
    },
    // 关闭修改商品数据框
    closeUpdateForm() {
      this.$refs.updateFormRef.resetFields()
    },
    // 点击删除商品
    async delGoods(row) {
      const result = await this.$confirm('你确定删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return
      const { data: res } = await this.$http.delete('goods/' + row.goods_id)
      if (res.meta.status !== 200) {
        return this.$msg.error('删除失败')
      }
      this.$msg.success('删除成功')
      await this.getGoodsList()
    }
  }
}
</script>

<style lang='less' scoped>
.col-search {
  margin-right: 15px;
}

.row-page, .row-tab {
  margin-top: 15px;
  text-align: center;
}
</style>
